#!/usr/bin/env ruby
#
# This file is gererated by ruby-glade-create-template 1.1.4.
#
require 'libglade2'
require 'grouhbot.rb'
require 'gnome2'

class RobobuilderGuiGlade
	include GetText

	attr :glade
	attr :connected
	attr :r
	attr :timer
	attr :servo_spin_ids

	def initialize(path_or_data, root = nil, domain = nil, localedir = nil, flag = GladeXML::FILE)
		bindtextdomain(domain, localedir, nil, "UTF-8")
		@glade = GladeXML.new(path_or_data, root, domain, localedir, flag) {|handler| method(handler)}
		@connected = false
		@psd_enabled = false
		@snd_sampling = false
		@handlers = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
		@servo_spin_ids = [
			"s00_spin", "s01_spin", "s02_spin", "s03_spin", "s04_spin",
			"s05_spin", "s06_spin", "s07_spin", "s08_spin", "s09_spin",
			"s10_spin", "s11_spin", "s12_spin", "s13_spin", "s14_spin",
			"s15_spin"  
		]
	end
	
	

	def logg(txt)
		@glade["serialport_log"].buffer.text += txt + "\n"
	end

	def updatePositions( pos )
		@servo_spin_ids.each_index {|index|
			@glade[@servo_spin_ids[index]].signal_handler_block(@handlers[index])
			@glade[@servo_spin_ids[index]].set_value( pos[index] )
			@glade[@servo_spin_ids[index]].signal_handler_unblock(@handlers[index])
		}
	end 

	def grouhbot_main
		@r.servoID_readservo
		pos = @r.getPos()
		updatePositions pos
		updatePSD if @psd_enabled
		updateSound if @snd_sampling
		updateAccelerometer
	end
	
	def updateAccelerometer
		xyz = @r.dcmpAccelerometer
		@glade["accel_x_valueLabel"].text = xyz[0].to_s
		@glade["accel_y_valueLabel"].text = xyz[1].to_s
		@glade["accel_z_valueLabel"].text = xyz[2].to_s
		@glade["accel_x"].value = xyz[0]
		@glade["accel_y"].value = xyz[1]
		@glade["accel_z"].value = xyz[2]
	end
	
	def updateSound
		lvl = @r.dcmpGetMicLevel
		@glade["snd_vumetter"].set_fraction((lvl[0] + lvl[1]).to_f / 10.0 )
	end
	
	def updatePSD
		dst = @r.dcmpPSD
		@glade["psd_distance_pb"].set_fraction( dst.to_f / 50.0 )
	end
	
	def servo_spin_changed(widget, servoID)
		begin
			if widget.value <= 360 and widget.value >= 0 
				@r.wckMovePos servoID, widget.value, 1
			end
		rescue Exception => e
			logg "ERROR while update servo #{servoID}: #{e.message}"
		end
	end

	def on_connect_btn_clicked(widget)
		serialPort = @glade["serialport_txtfield"].text
		if not @connected
			logg "Trying to connect to port #{serialPort}.."
			begin
				@r = GrouhBot.new( serialPort, true )
				logg "GrouhBot Connected !"
				#@glade["connect_btn"].text = "Disconnect"
				@connected = true
				
				@servo_spin_ids.each_index {|index|
					@handlers[index] = @glade[@servo_spin_ids[index]].signal_connect("value-changed"){servo_spin_changed(@glade[@servo_spin_ids[index]], index) }
				}
				
				@timer = Gtk.timeout_add(333) { grouhbot_main; }
			rescue Exception => e
				logg "ERROR: #{e.message}"
				@connected = false
			end
		else
			logg "Disconnecting robot"
			begin
				Gtk.timeout_remove @timer
				@r.close
				 #@glade["connect_btn"].text = "Connexion"
				@connected = false
			rescue Exception => e
				logg "ERROR: #{e.message}"
				@connected = true
			end
		end
	end

	def on_psd_enabled_toggled(widget)
		@psd_enabled = !@psd_enabled 	
		@r.dcmpPSD( @psd_enabled )
	end

	def on_snd_sampling_toggled(widget)
		@snd_sampling = !@snd_sampling
		@r.dcmpSetSampling( @snd_sampling )
	end
	def on_ruby_cmdline_key_release_event(widget, arg0)
		logg "on_ruby_cmdline_key_release_event() is not implemented yet."
	end
end

# Main program
if __FILE__ == $0
	# Set values as your own application. 
	PROG_PATH = "RoboBuilder_GUI.glade"
	PROG_NAME = "YOUR_APPLICATION_NAME"
	RobobuilderGuiGlade.new(PROG_PATH, nil, PROG_NAME)
	Gtk.main

	@glade["window"].signal_connect('destroy') do
		Gtk.main_quit
	end
end
